import { Logger } from '@nestjs/common';

const handleObj = (val: any) =>
  typeof val == 'object' && val !== null ? JSON.stringify(val) : val || '';
export default class FileLogger {
  private systemLogger: Logger;
  constructor(file: string) {
    this.systemLogger = new Logger(file);
  }
  printError(func: string, err: any) {
    this.systemLogger.error(`${func} catch error: ${err}`);
  }
  printInfo(func: string, arg1?: any, arg2?: any) {
    this.systemLogger.verbose(
      `${func}: ${handleObj(arg1)}; ${handleObj(arg2)}`,
    );
  }
  printParam(func: string, paramsName: string, params: any) {
    this.systemLogger.debug(
      `${func} 获取参数:  ${paramsName} ==> ${handleObj(params)}`,
    );
  }
  printWarn(func: string, arg1?: any, arg2?: any) {
    this.systemLogger.warn(`${func}:  ${handleObj(arg1)}; ${handleObj(arg2)}`);
  }
  printResp(
    func: string,
    type: 'SUCCESS' | 'FAIL' | 'ERROR',
    data: any,
    respData: any = '',
  ) {
    const dataInFo =
      `[${type}] ${JSON.stringify(data)}` + JSON.stringify(respData) ||
      ` RESPONSE: ${JSON.stringify(respData)}`;
    const PRINTTEXT = `${func}: ${dataInFo}`;
    if (type === 'SUCCESS') {
      this.systemLogger.log(PRINTTEXT);
    } else if (type === 'FAIL') {
      this.systemLogger.warn(PRINTTEXT);
    } else if (type) {
      this.systemLogger.error(PRINTTEXT);
    }
  }
}
